package com.lineage.data.npc.event;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lineage.data.cmd.CreateNewItem;
import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;

/**
 * 龍血兌換員<BR>
 * 71800<BR>
 * 
 * @author juonena
 * 
 */
public class Npc_tw_blood extends NpcExecutor {

	private static final Log _log = LogFactory.getLog(Npc_tw_blood.class);

	private Npc_tw_blood() {
		// TODO Auto-generated constructor stub
	}

	public static NpcExecutor get() {
		return new Npc_tw_blood();
	}

	@Override
	public int type() {
		return 3;
	}

	@Override
	public void talk(final L1PcInstance pc, final L1NpcInstance npc) {
		try {
			/**
			 * <br>
			 * 你是為龍血而來的嗎？<br>
			 * 我能提供給你，但需要付出些代價。
			 * <br>
			 * <br><a link="tw_blood_3">以強化自我加速藥水交換龍血</a>  
			 * <br><a link="tw_blood_4">以勇敢藥水交換龍血</a> 
			 * <br><a link="tw_blood_5">以慎重藥水交換龍血</a> 
			 */
			pc.sendPackets(new S_NPCTalkReturn(npc.getId(), "tw_blood_1"));

		} catch (final Exception e) {
			_log.error(e.getLocalizedMessage(), e);
		}
	}

	@Override
	public void action(final L1PcInstance pc, final L1NpcInstance npc, final String cmd, final long amount) {
		try {
			boolean isCloseList = false;
			/**
			 * <p>須要強化自我加速藥水4個與1500金幣。<br>
			 * 另外提醒您「龍的血液」是不可堆疊、不可刪除的，<br>
			 * 還請留兌換的數量，以防持有道具超過上限。</p>
			 */
			if (cmd.equalsIgnoreCase("request tw mj Dragon Blood A")) { // 取得龍血
				int[] items = new int[] { 40018, 40308 };
				int[] counts = new int[] { 4, 1500 };
				int[] gitems = new int[] { 44213 }; // 龍的血液
				int[] gcounts = new int[] { 1 };
				isCloseList = getItem(pc, items, counts, gitems, gcounts);
			}
			
			/**
			 * <p>須要勇敢藥水5個與1500金幣。<br>
			 * 另外提醒您「龍的血液」是不可堆疊、不可刪除的，<br>
			 * 還請留兌換的數量，以防持有道具超過上限。</p>
			 */
			else if (cmd.equalsIgnoreCase("request tw mj Dragon Blood B")) { // 取得龍血
				int[] items = new int[] { 40014, 40308 };
				int[] counts = new int[] { 5, 1500 };
				int[] gitems = new int[] { 44213 }; // 龍的血液
				int[] gcounts = new int[] { 1 };
				isCloseList = getItem(pc, items, counts, gitems, gcounts);
			}
			
			/**
			 * <p>須要慎重藥水15個與1500金幣。<br>
			 * 另外提醒您「龍的血液」是不可堆疊、不可刪除的，<br>
			 * 還請留兌換的數量，以防持有道具超過上限。</p>
			 */
			else if (cmd.equalsIgnoreCase("request tw mj Dragon Blood C")) { // 取得龍血
				int[] items = new int[] { 40016, 40308 };
				int[] counts = new int[] { 15, 1500 };
				int[] gitems = new int[] { 44213 }; // 龍的血液
				int[] gcounts = new int[] { 1 };
				isCloseList = getItem(pc, items, counts, gitems, gcounts);
			}
			
			if (isCloseList) {
				// 關閉對話窗
				pc.sendPackets(new S_CloseList(pc.getId()));
			}

		} catch (final Exception e) {
			_log.error(e.getLocalizedMessage(), e);
		}
	}
	
	/**
	 * 交換道具
	 * 
	 * @param pc
	 * @param items
	 * @param counts
	 * 
	 * @return 是否關閉現有視窗
	 */
	private boolean getItem(final L1PcInstance pc, int[] items, int[] counts, int[] gitems, int[] gcounts) {
		// 需要物件不足
		if (CreateNewItem.checkNewItem(pc, items, // 需要物件
				counts) < 1) {// 傳回可交換道具數小於1(需要物件不足)
			return true;

		} else {// 需要物件充足
			// 收回需要物件 給予完成物件
			CreateNewItem.createNewItem(pc, items, counts, // 需要
					gitems, 1, gcounts);// 給予
			return true;
		}
	}
}
